import GameManager from "./GameManager";
import { BlockType } from "../game/BlockManager";
import MsgSystem from "../MsgSystem/MsgSystem";
import MsgDefine from "../MsgSystem/MsgDefine";
import PropManager from "../game/PropManager";

export default class GameData {
    //历史最高分
    private HighestScore = 0;

    //当前分数
    private curScore = 0;

    //万能方块的数量
    private universalNum = 0;

    //更换ui的数量
    private replaceNum = 0;

    //炸弹数量
    private bombNum = 0;

    //当前生成方块的位置类型
    private posType = 0;

    //生成方块时的位置类型--随机库
    private randomPosType = [];

    //生成方块的最大个数
    private maxHexagonCount;// 1 和2

    //随机库里的最大数字类型
    private maxType;

    //网格上的方块
    public hexagonVec = {};

    //当前生成的方块
    private randomHexagonVec = [];

    //是否复活过
    private isResurrection;

    //当前局唯一标识
    private curRoundGUID;

    //领取过的道具
    private receivedProps = [0, PropManager.INIT_PROP_COUNT, PropManager.INIT_PROP_COUNT];

    private receivedBombNum;

    //已领取的替换ui的数量
    private receivedReplaceNum;

    //已领取的万能方块的数量
    private receivedUniversalNum;

    //上分界面使用过的玩家uid
    private usedPlayerUID;

    //是否有存档数据
    private isHaveGameArchive;

    //宝箱视频剩余观看次数
    private chestVideoCount;

    //复活视频剩余观看次数
    private reliveVideoCount;

    //炸弹视频剩余观看次数
    private bombVideoCount;

    private curDate: Date;

    private soundSwitch: boolean = true;


    public InitData() {
        this.HighestScore = 0;
        this.curScore = 0;
        this.universalNum = 0;
        this.replaceNum = 0;
        this.bombNum = 0;
        this.posType = 0;
        this.maxType = 0;
        this.receivedReplaceNum = 0;
        this.receivedUniversalNum = 0;
        this.receivedBombNum = 0;
        this.maxHexagonCount = 0;
        this.hexagonVec = {};
        this.randomHexagonVec = [];
        this.randomPosType = [];
        this.isHaveGameArchive = false;
        this.isResurrection = false;
        this.curRoundGUID = "";
        this.receivedProps = [0, PropManager.INIT_PROP_COUNT, PropManager.INIT_PROP_COUNT];
        this.usedPlayerUID = [];
        this.soundSwitch = true;

        this.ReadHighestScore();
    }

    GetChestVideoCount() {
        return this.chestVideoCount;
    }

    GetReliveVideoCount() {
        return this.reliveVideoCount;
    }

    GetBombVideoCount() {
        return this.bombVideoCount;
    }

    SetChestVideoCount(count) {
        this.chestVideoCount = count;
    }

    SetReliveVideoCount(count) {
        this.reliveVideoCount = count;;
    }

    SetBombVideoCount(count) {
        this.bombVideoCount = count;;
    }

    SetHeighestScore(score) {
        this.HighestScore = score;

        this.SaveHighestScore();

        // //上传分数到服务器
        // Game.SendMessage.SendScoreMessage(score);

        // var KVDataList = this.GenerateCloudData();
        // WXHelper.UploadUserCloudStorage(KVDataList);
    }

    SetReceivedProps(index, add) {
        if (!this.receivedProps) {
            this.receivedProps = [0, PropManager.INIT_PROP_COUNT, PropManager.INIT_PROP_COUNT];
        }
        this.receivedProps[index] += add;
        if (this.receivedProps[index] < 0) {
            this.receivedProps[index] = 0;
        }

        this.SaveGameArchive();

    }

    initReceivedProps(receivedProps) {
        this.receivedProps = receivedProps;
        this.SaveGameArchive();
    }

    SetUsedPlayerUID(playerUID) {
        this.usedPlayerUID.push(playerUID);

        // this.SaveGameArchive();

    }

    SetReceivedBombNum(num) {
        this.receivedBombNum = num;
    }

    SetReceivedReplaceNum(num) {
        this.receivedReplaceNum = num;
    }



    SetReceivedUniversalNum(num) {
        this.receivedUniversalNum = num;
    }

    //设置当前局的唯一标识
    SetCurRoundGUID(guid) {
        this.curRoundGUID = guid;

        // this.SaveGameArchive();

    }

    SetIsResurrection(value) {
        this.isResurrection = value;

        // this.SaveGameArchive();

    }

    /**
     * 设置声音开关
     * @param b 
     */
    setSoundSwitch(b: boolean) {
        this.soundSwitch = b;
        this.SaveGameArchive();
    }

    getSoundSwitch(): boolean {
        return this.soundSwitch;
    }

    //设置当前分数
    SetCurScore(curScore) {
        this.curScore = curScore;

        this.SaveGameArchive();

        GameManager.getInstance().eventCenter.DispatchEvent("score_update");
    }

    //添加分数
    AddCurScore(score) {
        this.curScore += score;

        this.SaveGameArchive();

        GameManager.getInstance().eventCenter.DispatchEvent("score_update");

        this.updateScore(this.curScore);

        //更新历史最高分
        var curHighestScore = this.GetHeighestScore();
        var curScore = this.curScore;
        if (curScore > curHighestScore) {
            this.SetHeighestScore(curScore);
        }
    }

    //更新主界面左边超越谁谁谁的榜单
    private updateScore(score) {
        GameManager.getInstance().eventCenter.DispatchEvent("updateScore", score);
    }

    //设置万能块的数量
    SetUniversalNum(num) {
        this.universalNum = num;

        // this.SaveGameArchive();
    }

    //设置更换ui的数量
    SetReplaceNum(num) {
        this.replaceNum = num;

        // this.SaveGameArchive();
    }

    SetBombNum(num) {
        this.bombNum = num;

        // this.SaveGameArchive();
    }

    //重置上分界面使用过的玩家uid
    ResetUsedPlayerUID() {
        this.usedPlayerUID.length = 0;

        // this.SaveGameArchive();
    }

    //重置网格上的方块数据
    ResetHexagonData() {
        this.hexagonVec = null;

        this.hexagonVec = {};

        // this.SaveGameArchive();
    }


    //重置当前随机生成的方块数据
    ResetRandomHexagonData() {
        this.randomHexagonVec.length = 0;

        this.SaveGameArchive();
    }

    //设置网格上的方块
    AddHexagon(type, id) {
        if (type >= BlockType.HT_2048) {
            type = 0;
        }
        if (this.hexagonVec) {
            this.hexagonVec[id] = type;
        } else {
            this.ResetHexagonData();
        }

        this.SaveGameArchive();
    }

    //设置当前随机生成的方块
    AddRandomHexagon(type: any[]) {
        if (this.randomHexagonVec) {
            this.randomHexagonVec = [];
            this.randomHexagonVec.push(...type);
        }

        this.SaveGameArchive();
    }

    //设置当前随机生成的方块的位置类型
    SetCurPosType(type) {
        this.posType = type;

        this.SaveGameArchive();
    }


    //设置生成方块时的最多个数
    SetMaxHexagonCount(count) {
        this.maxHexagonCount = count;

        // this.SaveGameArchive();
    }

    //设置方块的位置类型
    SetHexagonPosType(posType) {
        if (this.randomPosType) {
            this.randomPosType.length = 0;

            this.randomPosType = posType;
        }

        // this.SaveGameArchive();
    }

    GetReceivedBombNum() {
        return this.receivedBombNum;
    }

    GetReceivedReplaceNum() {
        return this.receivedReplaceNum;
    }

    GetReceivedUniversalNum() {
        return this.receivedUniversalNum;
    }

    GetReceivedProps() {
        return this.receivedProps;
    }

    GetUsedPlayerUID() {
        return this.usedPlayerUID;
    }

    //获取当前局的唯一标识
    GetCurRoundGUID() {
        return this.curRoundGUID;
    }

    GetIsResurrection() {
        return this.isResurrection;
    }

    //获得生成方块的最多个数
    GetMaxHexagonCount() {
        return this.maxHexagonCount;
    }

    //获取方块的位置类型
    GetHexagonPosType() {
        return this.randomPosType;
    }

    //获取当前分数
    GetCurScore() {
        return this.curScore;
    }

    //设置当前随机生成的方块的位置类型
    GetCurPosType() {
        return this.posType;
    }

    //设置随机库最大生成类型
    GetMaxType() {
        return this.maxType;
    }

    //设置万能块的数量
    GetUniversalNum(num) {
        return this.universalNum;
    }

    //设置更换ui的数量
    GetReplaceNum(num) {
        return this.replaceNum;
    }

    //获得炸弹数量
    GetBombNum(num) {
        return this.bombNum;
    }

    //获取最高分
    GetHeighestScore() {
        return this.HighestScore;
    }

    //
    GetHexagonData() {
        return this.hexagonVec;
    }

    GetRandomHexagon() {
        return this.randomHexagonVec;
    }

    //是否有存档
    IsHasGameArchive() {
        return this.isHaveGameArchive;
    }

    //读取最高分
    ReadHighestScore() {
        this.HighestScore = GameManager.getInstance().gameLocalData.ReadHighestScore();
        return this.HighestScore;
    }

    //重置存档数据
    ResetGameArchive() {
        this.InitData();
        this.onResetGameArchive();
    }

    //读取游戏存档
    ReadGameArchive() {
        var archiveData = this.onReadGameArchive();
        if (archiveData) {
            this.curScore = archiveData.cur_score;
            this.posType = archiveData.pos_type;
            this.maxType = archiveData.max_type;
            this.maxHexagonCount = archiveData.max_hexagon_count;
            this.universalNum = archiveData.universal_num;
            this.replaceNum = archiveData.replace_num;
            this.bombNum = archiveData.bomb_num;
            this.hexagonVec = archiveData.hexagon_data;
            this.randomHexagonVec = archiveData.random_hexagon_data;
            this.randomPosType = archiveData.random_pos_lib;
            this.isResurrection = archiveData.is_resurrection;
            this.curRoundGUID = archiveData.cur_round_guid;
            this.receivedProps = archiveData.received_props;
            this.usedPlayerUID = archiveData.used_player_uid;
            this.receivedReplaceNum = archiveData.received_replace_num;
            this.receivedUniversalNum = archiveData.received_universal_num;
            this.soundSwitch = archiveData.sound_switch;

            if (archiveData.received_bomb_num == undefined) {
                archiveData.received_bomb_num = 0;
            }

            this.receivedBombNum = archiveData.received_bomb_num;
        } else {
            this.InitData();
        }
        this.isHaveGameArchive = (archiveData != null);

        //读取最高分
        this.ReadHighestScore();
    }

    //保存最高分
    SaveHighestScore() {
        GameManager.getInstance().gameLocalData.SaveHighestScore(this.HighestScore)
    }

    //保存存档数据
    SaveGameArchive() {
        var archiveData = this.GenerateGameArchive();
        this.onSaveGameArchive(archiveData);
    }

    Save() {
        //保存到本地
        this.SaveHighestScore();
        this.SaveGameArchive();
    }

    //生成用户托管数据
    GenerateCloudData() {
        var cloudData = {
            key: "highest_score",
            value: this.HighestScore.toString(),
        }

        var keyDataList = [];

        keyDataList.push(cloudData);

        return keyDataList;
    }

    //生成用户存档数据
    GenerateGameArchive() {

        var archiveData = {
            cur_score: this.curScore,

            universal_num: this.universalNum,

            replace_num: this.replaceNum,

            bomb_num: this.bombNum,

            hexagon_data: this.hexagonVec,

            random_hexagon_data: this.randomHexagonVec,

            pos_type: this.posType,

            max_type: this.maxType,

            max_hexagon_count: this.maxHexagonCount,

            random_pos_lib: this.randomPosType,

            is_resurrection: this.isResurrection,

            cur_round_guid: this.curRoundGUID,

            received_props: this.receivedProps,

            used_player_uid: this.usedPlayerUID,

            received_replace_num: this.receivedReplaceNum,

            received_universal_num: this.receivedUniversalNum,

            received_bomb_num: this.receivedBombNum,

            sound_switch: this.soundSwitch
        };

        return archiveData;
    }

    //存档
    public onSaveGameArchive(data) {
        var strData = JSON.stringify(data);

        GameManager.getInstance().gameLocalData.SaveGameArchive(strData);
    }

    //读取游戏存档
    onReadGameArchive() {
        var strData = GameManager.getInstance().gameLocalData.ReadGameArchive();
        if (strData) {
            try {
                return JSON.parse(strData);
            } catch (err) {
                return null;
            }

        }
        return null;
    }

    //重置存档数据
    onResetGameArchive() {
        GameManager.getInstance().gameLocalData.RemoveGameArchive();
    }

    //UI更新读档数据 
    updateUIArchive() {
        GameManager.getInstance().eventCenter.DispatchEvent("readArchive");
    }
};

